<?php

	$partialcache 	= array();
 	 
	function redirect($newLocation){
		$host	= $_SERVER["HTTP_HOST"];
		$dir	= rtrim(dirname($_SERVER["PHP_SELF"]), '/\\');
		header("Location: http://$host$dir/$newLocation");
		exit;
	}
	
	/**
	 * 
	 */	
	function dump($arr){
		return '<pre>'.print_r($arr,true).'</pre>';		
	}
	
	/**
	 * 
	 */
	function filter($v){
		$v = stripslashes($v);	
		
		$v = preg_replace('#(<.*?>).*?(</.*?>)#', '$1$2$3', $v);
		$v = filter_var($v, FILTER_SANITIZE_STRIPPED);
		$v = trim($v);
		$v = str_replace(array('"',"'",';',':','\\','<','>'),'',$v);	
		return $v;
	}
	
	/**
	 * 
	 */
	function getParams($key = false){
		$params = array_merge($_GET,$_POST);
		if($key==false){
			return $params;
		} else {
			return $params[$key];
		}
	}

	/**
	 * 
	 */
	function partial($page,$action=array(),$path=false){
			
		if(!$page)
			return;
			
		if($path==false){
			$file = './source/view/'.$page.'.phtml';
		} else {
			$file = './source/view/'.$path.'/'.$page.'.phtml';
		}
		
		$key 	= md5($file);
		
		if(file_exists($file)){
			if(!$partialcache[$key]){
				$source = implode(file($file));
				$partialcache[$key] = $source;
			}
			
			ob_start();
			eval("?>" . $partialcache[$key] . "<?");
			$c = ob_get_contents();
			ob_end_clean();
			return $c; 
		} 
	}
	
	/*
	 * 
	 */
	function strtoslug($string,$filter=false){
						
		$text = str_replace(array('ä','ü','ö','Ä','Ü','Ö'),array('ae','ue','oe','Ae','Ue','Oe'),$string);
	    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
	    $text = trim($text, '-');
	    if (function_exists('iconv')){
	        $text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
	    }
	    $text = strtolower($text);
	    $text = preg_replace('~[^-\w]+~', '', $text);
	    if (empty($text)) {
		    return false;
	    }
			
		if($filter==false)
			$filter = 'abcdefghijklmnopqrstuvwxyz0123456789/+-&?;';

		for($u=0; $u<strlen($text); $u++){
			for($f=0; $f<strlen($filter); $f++){
				if($text{$u}==$filter{$f}){
					$ub.=$text{$u};
				}
			}
		}
		
		if($ub{strlen($ub)-1}=='/') $ub = substr($ub, 0, -1);
		return $ub;
	}
	
	/**
	 * 
	 */
	function getUriPages($filename=false){
			
			if($filename==false) $filename = 'index.php';
			$filter = 'abcdefghijklmnopqrstuvwxyz0123456789@./+-_ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			
			$base	= str_replace($filename,'', urldecode($_SERVER['PHP_SELF']));
			
			if($base!='/') { 
				$uri	= str_replace($base, '', urldecode($_SERVER['REQUEST_URI']));
			} else {
				$uri	= substr(urldecode($_SERVER['REQUEST_URI']),1,strlen(urldecode($_SERVER['REQUEST_URI'])));
			}

			$ux = explode('?',$uri);
			if(count($ux>1))
				$uri=$ux[0];
			
			for($u=0; $u<strlen($uri); $u++){
				for($f=0; $f<strlen($filter); $f++){
					if($uri{$u}==$filter{$f}){
						$ub.=$uri{$u};
					}
				}
			}
			
			if($ub{strlen($ub)-1}=='/') $ub = substr($ub, 0, -1);
			return explode('/', $ub);
		}
	

	/*
	 * 
	 */
	function ksortBy($arr, $bykey){
		$newArr = array();
		if($arr){
			$n=0;	
			foreach($arr as $i){
				if(intval($i)>0){
					$newArr['k'.$i[$bykey].''.$n] = $i;
				} else { 
					$newArr[$i[$bykey]] = $i;
				}
				$n++;
			}
		}
		ksort($newArr);
		
		return $newArr;
	}

    /**
     * 
     * 
     */
     function getDuration($sec){
        $mm = $sec/60;
        $ms = explode('.',$mm);
        
        if($ms[0]>60){
            $mh = $ms[0]/60;
        }
        
        if(intval($ms[1])>0) {
            $m = $ms[0].':'.fillLeadingZero(round(floatval('0.'.$ms[1])*60));
        } else {
            $m = $ms[0].':00';
        } 
        
        return $m;
    }
     
     /**
      * 
      * 
      */
      function fillLeadingZero($num){
        if(strlen($num)==1)
            $num='0'.$num;
            
        return $num;
    }
      
      /**
       * 
       * 
       */
       function mondayFromCalendarWeek($cw,$year) {
        
            $first  = mktime(0,0,0,1,1,$year);
            $wday   = date('w',$first);
            
            if ($wday<= 4) {
                $monday = mktime(0,0,0,1,1-($wday-1),$year);
            } else {
                $monday = mktime(0,0,0,1,1+(7-$wday+1),$year); 
            }
            
            $firstmonday    = $monday;
            $mon_month      = date('m',$firstmonday);
            $mon_year       = date('Y',$firstmonday);
            $mon_days       = date('d',$firstmonday);
            $days           = ($cw-1)*7;
            $monday_cw      = mktime(0,0,0,$mon_month,$mon_days+$days,$mon_year);
            
            return $monday_cw;
        }


?>